package sw6.digipecs.widget;

import java.util.List;

import sw6.digipecs.R;
import sw6.digipecs.model.ExtImageInSentenceBoard;
import sw6.digipecs.widget.dragdrop.DragLayout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;

public class SentenceBoardExtImageAdapter extends BaseAdapter {
	
	// TODO This is kinda copy/paste of the other ExtImage-adapters :(

	private Context mContext;
	private List<ExtImageInSentenceBoard> mImageList;
	private LayoutInflater mLayoutInflater;
	private DragLayout mDragLayout;

	public SentenceBoardExtImageAdapter(Context context, List<ExtImageInSentenceBoard> images, DragLayout dl) {
		mContext = context;
		mImageList = images;
		mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mDragLayout = dl;
	}

	@Override
	public int getCount() {
		return mImageList.size();
	}

	@Override
	public Object getItem(int pos) {
		return mImageList.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		FrameLayout view;
		ExtImageView imageView;
		
		ExtImageInSentenceBoard image = mImageList.get(pos);
		
		// Create view
		if (image.isLocked()) {
			imageView = new ExtImageView(mContext);
		} else {
			final DragableExtImageView deiv = new DragableExtImageView(mContext);
			deiv.setDragLayout(mDragLayout);
			imageView = deiv;
		}
		
		// Convert view
		if (convertView == null) {
            view = (FrameLayout) mLayoutInflater.inflate(R.layout.placeholder, parent, false);
        } else {
        	view = (FrameLayout) convertView;
        	view.removeAllViews();
        }
		
		// Set image
		imageView.setImage(image);
		view.addView(imageView);
		
		return view;
	}

}
